<?php
require_once $_SESSION["app_root"]."config/app.config.php";


class EntityFieldBLL
{
	/**
	 * 
	 * @return array - a list of the fields associated to the parameter <i>uuid</i> $ID
	 * @param uuid $ID
	 * @param string $orderBy[optional]
	 */
	public function GetAllByEntityID($ID, $orderBy = null){
		$fields = array();
		$orderByStr ="";
		if($orderBy != null){
			$orderByStr = " ORDER BY `".$orderBy."`";
		}
		$query = "SELECT * FROM `entity_fields` WHERE `entity_id` = '".$ID."' ".$orderByStr;
		$db = GetDatabase();
		$rs = $db->Execute($query);
		$db->close();
		foreach ($rs as $row) {
			$field = $this->EntityFieldDataReader($row);
			array_push($fields, $field);
		}
		return $fields;
	}
	
	public static function GetByID($ID, $getExtFormProperties = false, $getCustomFormProperties = false, $getServerProperties = null){
		$fieldDao = new EntityFieldDAO();
		$field = $fieldDao->GetByID($ID, $getExtFormProperties, $getCustomFormProperties, $getServerProperties);
		return $field;
	}
	
	public function GetExtProperties($ID){
		$fieldDAO = new EntityFieldDAO();
		return $fieldDAO->GetExtProperties($ID);
	}
	
	public function EntityFieldDataReader($result){
		$entityField = new EntityFieldDVO(
											$result['id'],
											$result['entity_id'],
											$result['name'],
											$result['grid_header'],
											$result['type'],
											$result['db_field'],
											$result['form_location'],
											$result['grid_location']
											);
		return $entityField;
	}
	
	public function SavePropertyValue($fieldId, $name, $value, $target){
		$entityFieldDao = new EntityFieldDAO();
		switch($target){
			case "DefaultClientProps":
				$IsCustom = 0;
				$IsClient = 1;
				break;
			case "ServertProps":
				$IsCustom = 1;
				$IsClient = 0;
				break;
		}
		$entityFieldDao->SavePropertyValue($fieldId, $name, $value, $IsCustom, $IsClient);
	}
	
	public function NameIsUnique($name){
		
	}
	/**
	 * 
	 * @return Returns a status message array
	 * @param object $EntityID
	 * @param object $Type
	 * @param object $Name
	 * @param object $DbField
	 * @param object $FormLabel
	 * @param object $FormLocation
	 * @param object $GridHeader
	 * @param object $GridLocation
	 */
	public function Create($EntityID, $Type, $Name, $DbField, $FormLabel, $FormLocation, $GridHeader, $GridLocation){
		global $EntityFieldStatusEnum;
		$userID = $_SESSION["SessionUser"]->getID();
		$userGroupID = $_SESSION["SessionUser"]->getGroupID();
		$now = time();
		$entityFieldDvo = new EntityFieldDVO();
		$entityFieldDvo->setID(uuid());
		$entityFieldDvo->setEntityID($EntityID);
		$entityFieldDvo->setName($Name);
		$entityFieldDvo->setFieldType($Type);
		$entityFieldDvo->setDbField($DbField);
		$entityFieldDvo->setFormLabel($FormLabel);
		$entityFieldDvo->setFormLocation($FormLocation);
		$entityFieldDvo->setGridHeader($GridHeader);
		$entityFieldDvo->setGridLocation($GridLocation);
		$entityFieldDvo->setCreatedBy($userID);
		$entityFieldDvo->setCreatedDate($now);
		$entityFieldDvo->setModifiedBy($userID);
		$entityFieldDvo->setModifiedDate($now);
		$entityFieldDvo->setOwnerID($userID);
		$entityFieldDvo->setGroupID($userGroupID);
		$entityFieldDvo->setStatus($EntityFieldStatusEnum["Normal"]);
		$entityFieldDvo->setU(7);
		$entityFieldDvo->setG(7);
		$entityFieldDvo->setW(1);
		$entityFieldDao = new EntityFieldDAO();
		return $entityFieldDao->Create($entityFieldDvo);
	}
	
	public static function PharseForStorage($field){
		$field = EntityFieldDVO::cast($field);
		$value = $field->getValue();
		switch($field->getFieldType()){
			case "DateField":
				$value = strtotime($value);
				break;
		}
		$field->setValue($value);
		return $field;
	}
}
?>